package com.yu.util;

import java.util.List;
import java.util.Map;

import org.apache.poi.util.SystemOutLogger;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yu.model.Apply;
import com.yu.model.Club;
import com.yu.model.User;
import com.yu.model.UserCar;
import com.yu.model.UserClub;

public class ClubUtil {
	public static final String weixin_xiaomi = "chemengza"; 

	// 100 群内管理员讨论组 101 待购讨论组 102 保养讨论组 103 保险讨论组 104 聚会活动讨论组 1000 默认公共聊天室 1001 同城同车型聊天室 1002 话题聊天室 1003 特殊车型聊天室
	private static Club newSubClub(int type){
		Club club = new Club();
		club.set("type", type);
		club.set("parentid", 0);
		String name = null;
		boolean disabled = false;
		switch (type) {
			case 100:
				name = "群内管理员讨论组";
				disabled = true;
				break;
			case 101:
				name = "待购讨论组";
				disabled = true;
				break;
			case 102:
				name = "保养讨论组";
				disabled = true;
				break;
			case 103:
				name = "保险讨论组";
				disabled = true;
				break;
			case 104:
				name = "聚会活动讨论组";
				disabled = true;
				break;
			case 1000:
				name = "默认公共聊天室";
				break;
			case 1001:
				name = "同城同车型聊天室";
				break;
			case 1002:
				name = "话题聊天室";
				break;
			case 1003:
				name = "特殊车型聊天室";
				break;
			default:
				break;
		}
		club.set("name", name);
		//club.set("disabled", disabled);
		return club;
	}
	
	
	public static Map<Integer,Club> newSubClubMap(){
		Map<Integer,Club> result = Maps.newHashMap();
		result.put(100, newSubClub(100));
		result.put(101, newSubClub(101));
		result.put(102, newSubClub(102));
		result.put(103, newSubClub(103));
		result.put(104, newSubClub(104));
		/*
		result.put(1000, newSubClub(1000));
		result.put(1001, newSubClub(1001));
		result.put(1002, newSubClub(1002));
		result.put(1003, newSubClub(1003));
		*/
		return result;
	}

	public static final int MAX_CAN_JOIN_COUNT  = 5;
	
	public static final int STATUS_REFUSE = -1;
	
	public static final int STATUS_ADOPT = 1;
	
	public String clubApplyAuth(Apply apply){
		Long bid = Long.valueOf(apply.getInt("bid"));
		Long sid = Long.valueOf(apply.getInt("sid")) ;
		
		Club club = Club.dao.findById(apply.get("clubid"));
		apply.set("opuserid", club.get("userid"));
		// 获取用户已经加入的车友会数量
		UserClub userClub = UserClub.dao.findFirst("select count(*) as count from club_userclub as uc left join club_club as c on c.id=uc.clubid where uc.userid = "+apply.get("userid") +" and uc.valid=0 and c.valid=0 and c.type < 9");
		int num = Integer.valueOf(String.valueOf(userClub.get("count"))) ;
		if (num >= MAX_CAN_JOIN_COUNT) {
			// 不能加入，把status update为 -1 （ -1拒绝，0默认、等待，1 通过）
			apply.set("status", STATUS_REFUSE);
			apply.update();
			return "已有" + MAX_CAN_JOIN_COUNT +"个车友会";
		}

		// 获取用户提交申请时的车
		String unifea = bid + "_" + sid;
		List<UserCar> userCarlst = UserCar.dao.find("select * from club_usercar where userid = " + apply.get("userid"));
		long carid = -1;
		for (UserCar usercar : userCarlst) {
			// 说明有符合条件的汽车, 包括已购和未购			
			if (unifea.equals(usercar.getStr("unifea"))) {
				// 无论是否改过车型，直接让他入会，以当前的carid为准
				carid = usercar.getLong("carid");
				break;

			}
		}
		if (carid < 0) {
			// 不能加入，把status update为 -1 （ -1拒绝，0默认、等待，1 通过）
			apply.set("status", STATUS_REFUSE);
			apply.update();
			return "没有相关车型";
		} else {
			apply.set("status", STATUS_ADOPT);
			apply.update();
			try{
			UserClub newUserClub = new UserClub();
			newUserClub.set("admin", 0);
			newUserClub.set("auth", -1);
			newUserClub.set("carid", carid);
			newUserClub.set("clubid", apply.get("clubid"));
			newUserClub.set("auth", -1);
			newUserClub.set("holder", 0);
			newUserClub.set("userid", apply.get("userid"));
			newUserClub.set("createtime", System.currentTimeMillis());
			newUserClub.set("valid", 0);
			newUserClub.save();
			} catch (Exception e){
				e.printStackTrace();
			}
			List<String> userids = Lists.newArrayList();
			userids.add(String.valueOf(apply.get("userid")));
			new QQIMComponent().addGroupMembers(String.valueOf(apply.get("clubid")), userids);
			return "操作成功";
		}

	}


	public String transfer(String clubid, String userid) {
		User user = User.dao.findFirst("select * from club_users where userid = "+userid);
		if (user == null){
			return "userid 不存在";
		}
		Club club = Club.dao.findById(clubid);
		if (club == null){
			return "clubid 不存在";
		}
		UserClub userClub = UserClub.dao.findFirst("select * from club_userclub where valid > -1 and clubid ="+clubid+" and userid="+userid);
		if (userClub == null){
			return "userid :"+userid +"不在车友会"+clubid+"中";
		}
		UserClub adminUserClub =  UserClub.dao.findFirst("select * from club_userclub where valid > -1 and clubid ="+clubid+" and userid="+ club.get("userid") );
		adminUserClub.set("admin", 0);
		adminUserClub.set("holder", 0);
		adminUserClub.update();
		
		club.set("userid", userid);
		club.update();
		
		userClub.set("admin", 1);
		userClub.set("holder", 1);
		userClub.update();
		
		System.out.println(new QQIMComponent().changeGroupOwer(clubid, userid));
		return "操作成功";
	}
}
